<?php
/**
 * Requester
 * Querying data for the DataHandler
 * 
 * @license GNU/GPL 3
 * This package is distributed under the
 * GNU General Public License v.3.
 * See the LICENSE.txt file for details.
 * 
 * @package   Phlame
 * @author    Phlegma <phlegma@pheelgood.net>
 * @copyright 2008 by Phlegma, Phlame
 * @link 	  http://ugamela-forum.pheelgood.net
 * @since     29.06.2008
 * @version   0.1
 * @access    public
 * 
 */


abstract class Requester_old
{
	const DATA_TABLE = 'app_data';
	
	protected $used = false;
	protected $write_rights = false;
	protected $queried = false;
	
	protected $last_queried = array();
	
	protected $data = array();
	protected $data_wr = array();
	protected $data_memory = array();
	
	/**
	 * @var DB
	 */
	protected $DB;
	
	# (Multi-)Singleton
	protected static $Instances = array();
	/**
	 * @return Requester
	 */
	public static function getInstance() {
		$class = $this->getClassName();

        if (!array_key_exists($class, $instances)) {

            self::$Instances[$class] = new $class();
        }
        return self::$Instances[$class];
	}
	private final function __clone() {}
	
	protected function __construct() {
		$this->DB = DB::getInstance();
	}
	
	abstract function getClassName();
	
	/**
	 * Get datasets by id
	 *
	 * @param array $ids
	 * @return array datasets
	 */
	public function get(array $ids)
	{
		$ret = array();
		foreach ($ids as $id)
		{
			if (!isset($this->data[$id]))
			{
				throw new RequesterException('Unable to find requested id to get');
			}
			$ret[$id] = $this->data[$id];
		}
		return $ret;
	}
	
	/**
	 * Get the data from the last query
	 *
	 * @return array last_queried
	 */
	public function getLastQueried()
	{
		return $this->last_queried;
	}
	
	/**
	 * Request write acces for the next query
	 *
	 * @param bool $give
	 */
	public function write_acces($give = true)
	{
		/*if ($this->queried)
		{
			throw new RequesterException('You must attemp to get write acces before querying data');
		}*/
		$this->write_rights = (bool) $give;
	}
	
	/**
	 * Write the modified data array into the database
	 *
	 * @param array $data
	 * @return bool succes
	 */
	public function writeData(array $data)
	{
		if (!$this->used)
		{
			throw new RequesterException('No write-rights given, data cannot be written');
		}
		foreach ($data as $d)
		{
			$d['used'] = '0';
		}
		if (!$this->DB->actualize_array( $data, $this->data_backup, self::DATA_TABLE, 'id', $this->write_rights, true))
		{
			return false;
		}
		$this->data = $this->data_backup = $data;
		return true;
	}
	
	/**
	 * Queries data
	 * @param string $where
	 * @return array ids
	 */
	public function query($where = '')
	{
		if ($this->write_rights)
		{
			# wait till not used
			$wtnu = new WTNU(self::DATA_TABLE,$where);
			$wtnu->waitTillNotUsed();
		}
		$this->last_queried = $this->DB->getAll('SELECT * FROM `?pre'.self::DATA_TABLE .'`'.$where);
		$this->data = array_merge($this->data, $this->last_queried);
		$this->data_memory = array_merge($this->data_memory, $this->last_queried);
		$ids = array_keys($this->last_queried);
		if ($this->write_rights)
		{
			foreach ($ids as $id)
			{
				$this->data_wr[$id] = true;
			}
		}
		$this->write_rights = false;
		return $ids;
	}
	
	public function subAvailable()
	{
		return false; # Subs are turned off
	}
}
/*
Extendet Requester to support subs, dependent data
atm it only supports one sub, but the datahandler is able to handle multiple nested subs
*/
abstract class SubRequester_old extends Requester_old
{
	const SUB_CLASS = 'Sub';
	const SUB_TABLE = 'Subs';
	
	protected $subqueried = false;
	protected $subcount = 1;
	protected $last_sub_queried;
	
	protected $subs = array();
	protected $subs_memory = array();
	
	
	public function subAvailable() {
		
		return $this->subqueried && $this->subcount--; # subcount == 0 -> false
	}
	
	public function subclass() {
		return self::SUB_CLASS;
	}
	
	public function querySub() {
		$this->subqueried = true;
		$ids = array_keys($this->last_queried);
		$this->last_sub_queried = $this->DB->getAll('SELECT * FROM `?pre'.self::SUB_TABLE.'` WHERE `id` IN ('.implode(',',$ids).')');
		$this->subs = array_merge($this->subs, $this->last_sub_queried);
		$this->subs_memory = array_merge($this->subs_memory, $this->last_sub_queried);
		return $this->last_sub_queried;
	}
}